﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceZero.Business
{
    public class BusinessBase
    {
        /// <summary>
        /// 批量动作实现
        /// </summary>
        /// <typeparam name="ArgsType">参数类型</typeparam>
        /// <typeparam name="ResultType">返回结果类型</typeparam>
        /// <param name="argsList">参数列表</param>
        /// <param name="action">针对每个参数执行的动作</param>
        /// <returns></returns>
        protected List<ResultType> BatchAction<ArgsType, ResultType>(List<ArgsType> argsList, Func<ArgsType, ResultType> action)
        {
            List<ResultType> list = new();
            foreach (var args in argsList)
            {
                list.Add(action(args));
            }
            return list;
        }

        /// <summary>
        /// 批量动作实现
        /// </summary>
        /// <typeparam name="ArgsType">参数类型</typeparam>
        /// <param name="argsList">参数列表</param>
        /// <param name="action">针对每个参数执行的动作</param>
        /// <returns></returns>
        protected void BatchAction<ArgsType>(List<ArgsType> argsList, Action<ArgsType> action)
        {
            foreach (var args in argsList)
            {
                action(args);
            }
        }

        protected void LockAction(object lockObj, Action action)
        {
            if (lockObj != null)
            {
                lock (lockObj)
                {
                    if (lockObj != null)
                    {
                        action();
                    }
                }
            }
        }

    }
}
